/*
Copyright (C) 2013 Johan Ceuppens

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#import "Trackable.h"
#import "FunctorCl.h"
/*
@implementation Trackable

- (id) init
{
	callbacklist = [NSMutableArray new];
	iterator = [callbacklist objectEnumerator];
	return self;
}

- (void)addCallback:(SIGCMType*)d fun:(void*)f
{
	struct Callback *cb = (struct Callback*)malloc(sizeof(struct Callback));
	cb->data = d;
	cb->fun = f;	
	[callbacklist addObject:(id)cb];
}

- (void)track
{
	//emit all callbacks
	id object;
	while (object = [iterator nextObject]) {
		(((struct Callback*)object)->fun(nil,nil));//FIXME
	}

	[iterator dealloc];//FIXMENOTE set to begin
	iterator = [callbacklist objectEnumerator];
}

@end
*/

@implementation Trackable
- (id) init
{
	callbacklist = [NSMutableArray new];
	iterator = [callbacklist objectEnumerator];
	ffactory = [FunctorFactory new];
	return self;
}

- (void)addFunctor:(int)nArgs cFunction:(void*)f
{
	[callbacklist addObject:
		[ffactory makeFunctorWithArgNumber:nArgs cFunction:f]];

	//FIXME recreate iterator ?
	[iterator dealloc];//FIXMENOTE set to begin
	iterator = [callbacklist objectEnumerator];
}
 
//note array must match all arguments of all in callbacklist (whic are functors)
- (void)track:(NSMutableArray*)array
{
	NSEnumerator *ai = [array objectEnumerator];
	//emit all callbacks
	id object;
	while (object = [iterator nextObject]) {
		if ([object isKindOfClass:[Functor0 class]])
			[object dof];	
		else if ([object isKindOfClass:[Functor1 class]])
			[object dof:[ai nextObject] arg1:[ai nextObject]];	
		else if ([object isKindOfClass:[Functor2 class]])
			[object dof:[ai nextObject] arg1:[ai nextObject] arg2:[ai nextObject]];	
	}

	[iterator dealloc];//FIXMENOTE set to begin
	iterator = [callbacklist objectEnumerator];
}

- (void)clear
{
	//FIXME clear arrays
}

@end
